package com.wuneng.commonlibrary.net;

import android.content.Context;

import com.wuneng.commonlibrary.util.JsonUtils;

import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.FileCallBack;
import com.zhy.http.okhttp.callback.StringCallback;

import java.io.File;
import java.util.Map;

import okhttp3.Call;
import okhttp3.MediaType;

/**
 * 请求工具类
 */
public class RequestUtils {

    public static void getRequest(Context context, String url, Map<String, String> params, Map<String, String> headers, final RequestCallback callback, final int tag) {
        OkHttpUtils
                .get()
                .url(url)
                .params(params)
                .headers(headers)
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        callback.onRequestError(tag);
                    }

                    @Override
                    public void onResponse(String response, int id) {
                        ServerResponse serverResponse = JsonUtils.getObj(response, ServerResponse.class);
                        if (serverResponse.getCode() == 0) {
                            callback.onRequestFail(tag, serverResponse.getMessage());
                        } else {
                            callback.onRequestSuccess(tag, serverResponse.getData());
                        }
                    }
                });
    }


    public static void postRequest(Context context, String url, String params, Map<String, String> headers, final RequestCallback callback, final int tag) {
        OkHttpUtils
                .postString()
                .url(url)
                .headers(headers)
                .mediaType(MediaType.parse("application/json; charset=utf-8"))
                .content(params)
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        callback.onRequestError(tag);
                    }

                    @Override
                    public void onResponse(String response, int id) {
                        ServerResponse serverResponse = JsonUtils.getObj(response, ServerResponse.class);
                        if (serverResponse.getCode() == 0) {
                            callback.onRequestFail(tag, serverResponse.getMessage());
                        } else {
                            callback.onRequestSuccess(tag, serverResponse.getData());
                        }
                    }
                });
    }

    public static void downloadFile(Context context, String url, Map<String,String> headers,String path, String fileName, final DownloadCallback callback, final int tag){
        OkHttpUtils
                .get()
                .url(url)
                .headers(headers)
                .build()

                .execute(new FileCallBack(path, fileName) {

                    @Override
                    public void inProgress(float progress, long total, int id) {
                        callback.onProgress(progress,total,tag);
                    }

                    @Override
                    public void onError(Call call, Exception e, int id) {
                        callback.onDownloadError(e, tag);
                    }

                    @Override
                    public void onResponse(File response, int id) {
                        callback.onDownloadFinish(response,tag);
                    }
                });
    }


    public static void UpFile(Context context, String url, Map<String,String> headers,File file, final UploadCallback callback, final int tag){
        OkHttpUtils
                .postFile()
                .url(url)
                .headers(headers)
                .file(file)
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void inProgress(float progress, long total, int id) {
                        callback.onProgress(progress,total,tag);
                    }

                    @Override
                    public void onError(Call call, Exception e, int id) {
                        callback.onUploadError(e,tag);
                    }

                    @Override
                    public void onResponse(String response, int id) {
                        callback.onUploadFinish(response,tag);
                    }
                });
    }
}
